package com.jiangjiang.eshop.auth.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jiangjiang.eshop.auth.domain.PriorityEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @ClassName PriorityMapper
 * @Description TODO
 * @Author jhw
 * @Date 2021/4/20 21:18
 * @Version 1.0
 **/
@Mapper
public interface PriorityMapper extends BaseMapper<PriorityEntity> {

    /**
     * 根据用户账户id查询关联的权限数据
     *
     * @param condition 查询条件
     * @return
     */
    @Select("<script>"

            + "SELECT "
                + "id,"
                + "code,"
                + "url,"
                + "priority_comment,"
                + "priority_type,"
                + "parent_id,"
                + "gmt_create,"
                + "gmt_modified "
            + "FROM "
            + "("
                + "SELECT "
                    + "id,"
                    + "code,"
                    + "url,"
                    + "priority_comment,"
                    + "priority_type,"
                    + "parent_id,"
                    + "gmt_create,"
                    + "gmt_modified "
                + "FROM auth_account_role_relationship acr "
                + "INNER JOIN auth_role_priority_relationship rpr ON acr.role_id=rpr.role_id "
                + "INNER JOIN auth_priority p ON rpr.priority_id=p.id "
                + "WHERE acr.account_id=#{accountId} "

                + "<if test='parentId == null'>"
                + "AND p.parent_id IS NULL "
                + "AND p.priority_type=1 "
                + "</if>"

                + "<if test='parentId != null'>"
                + "AND p.parent_id=#{parentId} "
                + "</if>"

                + "UNION ALL"

                + "SELECT "
                    + "id,"
                    + "code,"
                    + "url,"
                    + "priority_comment,"
                    + "priority_type,"
                    + "parent_id,"
                    + "gmt_create,"
                    + "gmt_modified "
                + "FROM auth_account_priority_relationship apr "
                + "JOIN auth_priority p ON apr.priority_id=p.id "
                + "WHERE apr.account_id=#{accountId} "


                + "<if test='parentId == null'>"
                + "AND p.parent_id IS NULL"
                + "AND p,priority_type=1"
                + "</if>"

                + "<if>"
                + "AND p.parent_id=#{parentId}"
                + "</if>"
            + ") a"

            + "</script>"
    )
    List<PriorityEntity> listAuthorizedTreeByAccountCondition(Map<String, Object> condition);


    /**
     * 根据权限URL判断账号是否对这个权限有授权记录
     * @param accountId 用户账户
     * @param url 权限url
     * @return 查询结果
     */
    @Select(
            "SELECT SUM(cnt)"
            + "FROM "
            + "("
                + "SELECT count(*) cnt"
                + "FROM auth_account_role_relationship arr "
                + "INNER JOIN auth_role_priority_relationship rpr ON arr.role_id=rpr.role_id "
                + "INNER  auth_priority p ON rpr.priority_id=p.id "
                + "WHERE arr.account_id=#{accountId} "
                + "AND p.url=#{url}"

                + "UNION ALL"

                + "SELECT count(*) cnt "
                + "FROM auth_account_priority_relationship apr "
                + "INNER JOIN auth_priority p ON apr.priority_id = p.id "
                + "WHERE apr.account_id=#{account_id} "
                + "AND p.url=#{url}"
            + ") a"

    )
    Long countAuthorizedByUrl(@Param("accountId") Long accountId, @Param("url") String url);

    /**
     * 根据权限code判断账号是否对这个权限有授权记录
     * @param accountId 用户账户
     * @param code 权限编号
     * @return 查询结果
     */
    @Select(
            "SELECT SUM(cnt)"
                    + "FROM "
                    + "("
                    + "SELECT count(*) cnt"
                    + "FROM auth_account_role_relationship arr "
                    + "INNER JOIN auth_role_priority_relationship rpr ON arr.role_id=rpr.role_id "
                    + "INNER  auth_priority p ON rpr.priority_id=p.id "
                    + "WHERE arr.account_id=#{accountId} "
                    + "AND p.code=#{code}"

                    + "UNION ALL"

                    + "SELECT count(*) cnt "
                    + "FROM auth_account_priority_relationship apr "
                    + "INNER JOIN auth_priority p ON apr.priority_id = p.id "
                    + "WHERE apr.account_id=#{account_id} "
                    + "AND p.code=#{code}"
                    + ") a"

    )
    Long existAuthorizedByCode(@Param("accountId") Long accountId, @Param("code") String code);

    /**
     * 根据权限id查询与之绑定的账号id
     *
     * @param priorityId 权限id
     * @return 查询结果
     */
    @Select("SELECT DISTINCT(account_id) "
            + "FROM ("
                +"SELECT arr.account_id"
                + "FROM auth_account_role_relationship arr "
                + "JOIN auth_role_priority_relationship rpr ON arr.role_id = rpr.role_id "
                + "WHERE rpr.priority_id=#{priorityId} "

                + "UNION ALL "

                + "SELECT apr.account_id"
                + "FROM auth_account_priority_relationship apr "
                + "WHERE apr.priority_id=#(priorityId}"
            + ") a" )
    List<Long> listAccountIdsByPriorityId(@Param("priorityId") Long priorityId);
}
